package com.example.foodsy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foodsy.dto.AddressDTO;
import com.example.foodsy.entity.CompanyInfo;
import com.example.foodsy.entity.RetaInfo;
import com.example.foodsy.entity.SlauInfo;
import com.example.foodsy.entity.WholInfo;
import com.example.foodsy.mapper.CompanyInfoMapper;
import com.example.foodsy.mapper.RetaInfoMapper;
import com.example.foodsy.mapper.WholInfoMapper;
import com.example.foodsy.service.RetaInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.foodsy.utils.Result;
import com.example.foodsy.utils.TraceCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 零售商产品批号信息表 服务实现类
 * </p>
 *
 * @author cgc
 * @since 2025-09-23
 */
@Service
public class RetaInfoServiceImpl extends ServiceImpl<RetaInfoMapper, RetaInfo> implements RetaInfoService {
	@Autowired
	private TraceCodeUtils traceCodeUtils; // 溯源码工具类
	@Autowired
	private WholInfoMapper wholInfoMapper;

	@Autowired
	private CompanyInfoMapper companyInfoMapper;
	@Override
	public AddressDTO<RetaInfo> getInfoByQR(String qrCode) {
		AddressDTO<RetaInfo> addressDTO = new AddressDTO<>();

		RetaInfo retaInfo = baseMapper.selectOne(new QueryWrapper<RetaInfo>().eq("source_qr", qrCode));

		addressDTO.setCompanyInfo(retaInfo);
		addressDTO.setCompanyName(companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", retaInfo.getRetaId())).getName());

		return addressDTO;
	}

	@Override
	public boolean addRetaInfo(RetaInfo retaInfo) {
		boolean save = save(retaInfo);
		return save;
	}

	@Override
	public AddressDTO<WholInfo> getWholInfo(Integer wbId) {
		WholInfo wholInfo = wholInfoMapper.selectOne(new QueryWrapper<WholInfo>().eq("wb_id", wbId));

		CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", wholInfo.getWholId()));

		AddressDTO<WholInfo> addressDTO = new AddressDTO<>();
		addressDTO.setOriginBatchId(wholInfo.getBatchId());
		addressDTO.setCompanyName(companyInfo.getName());

		return addressDTO;
	}

	@Override
	public AddressDTO<WholInfo> getRetaInfo(String wbId) {
		WholInfo wholInfo = wholInfoMapper.selectOne(new QueryWrapper<WholInfo>().eq("wb_id", wbId));

		CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", wholInfo.getWholId()));

		AddressDTO<WholInfo> addressDTO = new AddressDTO<>();

		addressDTO.setOriginBatchId(wholInfo.getBatchId());
		addressDTO.setCompanyName(companyInfo.getName());
		addressDTO.setCompanyInfo(wholInfo);
		addressDTO.setCityId(companyInfo.getCityId());
		addressDTO.setProvinceId(companyInfo.getProvId());
		addressDTO.setCompanyId(companyInfo.getId());

		return addressDTO;
	}


	@Override
	/**
	 * 状态变更方法，当状态改为 “已确认” 时，调用溯源码生成工具类生成source_id，并更新reta_batch表
	 * 零售商确认产品批号（触发溯源码生成）
	 * @param rbId 零售商批号ID
	 * @return 操作结果
	 */
	public Result confirmBatch(Integer rbId) {
		// 1. 校验批号存在且状态为“待确认”
		RetaInfo retaBatch = baseMapper.selectById(rbId);
		if (retaBatch == null) {
			return Result.fail("零售商批号不存在");
		}
		if (retaBatch.getState() != 2) { // 2=待确认
			return Result.fail("仅“待确认”状态的批号可确认");
		}

		// 2. 生成溯源码
		String sourceId = traceCodeUtils.generateSourceId(
				retaBatch.getRetaId(), // 零售商ID
				retaBatch.getBatchId() // 进货批号
		);

		// 3. 更新状态和溯源码
		retaBatch.setState(3); // 3=已确认
		retaBatch.setSourceId(sourceId);
		retaBatch.setBatchDate(LocalDate.now()); // 确认日期
		int rows = baseMapper.updateById(retaBatch);

		return rows > 0 ? Result.ok("确认成功，溯源码：" + sourceId) : Result.fail("确认失败");
	}
}

	@Override
	public List<RetaInfo> getRetaInfoBystate(String state, Integer companyId) {
		List<RetaInfo> list = new ArrayList<>();
		CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", companyId));

		List<RetaInfo> list1 = baseMapper.selectList(new QueryWrapper<RetaInfo>().eq("state", state));
		List<Integer> list2 = new ArrayList<>();
		for (RetaInfo retaInfo : list1) {
			list2.add(retaInfo.getWbId());
		}
		List<Integer> list3 = new ArrayList<>();
		for (Integer wbId : list2) {
			WholInfo wholInfo = wholInfoMapper.selectOne(new QueryWrapper<WholInfo>().eq("wb_id", wbId).eq("whol_id", companyId));
			list3.add(wholInfo.getWbId());
		}

		for (Integer wbId : list3) {
			RetaInfo retaInfo = baseMapper.selectOne(new QueryWrapper<RetaInfo>().eq("wb_id", wbId));
			list1.add(retaInfo);
		}

		return list1;
		}

	}



